﻿<%@ WebHandler Language="C#" Class="wgt" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
using App_Code;
using App_Code.Context;
using Jayrock.JsonRpc;
using Jayrock.JsonRpc.Web;

using WGT;

public class wgt : JsonRpcHandler, IRequiresSessionState
{
    public Dialog Current(Func<Dialog> factory)
    {
        var ctx = (Dialog)Session["dd"];
        if (ctx == null || factory != null)
        {
            ctx = factory();
            Session["dd"] = ctx;
            return ctx;
        }
        return ctx;
    }

    [JsonRpcMethod("show", Idempotent = true)]
    public List<string[]> show(string path, int ww, int wh, string cookie)
    {
        Func<Dialog> create = null;
        if (path == "main")
            if (Context.Request != null) 
                create = () => new dbvc_main(ww, wh, cookie.Split('=')[1], CreateState);
        var cnt = Current(create).Response();
        return cnt;
    }

    private dbvc_model CreateState()
    {
        if (HttpContext.Current.Session["ddstate"] == null) 
            HttpContext.Current.Session["ddstate"] = new dbvc_model();
        
        return (dbvc_model)HttpContext.Current.Session["ddstate"];
    }

    [JsonRpcMethod("event", Idempotent = true)]
    public List<string[]> onevent(params string[] args)
    {
        Dialog ctx = Current(null);
        ctx.event_args = args;
        ctx.OnCommand(args[0]);

        return ctx.Response();
    }
}